package model
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	/**
	 * Proxy对象,用于封装程序与外部的数据交互
	 * 注意Proxy对象只发送Notification,而不进行Notification的监听,为了提高重用性
	 * */
	public class HttpProxy extends Proxy
	{
		public static const NAME:String="httpProxy";
		public static const CONNSUCCESS:String="httpConnSuccess";
		public static const CONNFAULT:String="httpConnFault";
		private var httpConn:URLLoader=new URLLoader();
		public function HttpProxy(proxyName:String=null, data:Object=null)
		{
			trace("构造")
			super(NAME, data);
			httpConn.addEventListener(Event.COMPLETE,resultHandler);
			httpConn.addEventListener(IOErrorEvent.IO_ERROR,faultHandler);
		}
		
		/**
		 * 程序内的方法,使用HttpService连接的测试,供外部直接调用 
		 * */
		public function connUrl(url:String):void
		{
			var b:URLRequest=new URLRequest(url);
			b.method=URLRequestMethod.POST;
			httpConn.load(b);
		}
		
		/**
		 * 成功返回HttpServiced回调函数,同时发出Notification通知请求成功
		 * */
		public function resultHandler(e:Event):void
		{
		 	sendNotification(CONNSUCCESS,httpConn.data);
		}
		 
		/**
		 * 请求发生错误时候,同时发送对应的Notification通知
		 * */
	    public function faultHandler(e:IOErrorEvent):void
		{
		 	sendNotification(CONNFAULT,e.text.toString());
		}
		
	}
}